package com.jeasonzhao.report.engine.impl;

import java.util.ArrayList;

import com.jeasonzhao.commons.logger.Logger;
import com.jeasonzhao.commons.utils.Guid;
import com.jeasonzhao.commons.xml.HTMLNode;
import com.jeasonzhao.report.dataset.DataSet;
import com.jeasonzhao.report.engine.irp.IChartRender;
import com.jeasonzhao.report.exceptions.ChartException;
import com.jeasonzhao.report.model.Report;
import com.jeasonzhao.report.model.chart.Chart;

public abstract class AbstractChartHtmlRender implements IChartRender
{
    private StringBuffer m_buf = new StringBuffer();
    public AbstractChartHtmlRender()
    {
    }

    public String renderChartHtml(Logger log,Report report,DataSet dataset)
        throws ChartException
    {
        if(null == report
           || null == report.getChartModels()
           || report.getChartModels().size() < 1)
        {
            return "";
        }
        ArrayList<Integer> roweindexArray = new ArrayList<Integer>();
        java.util.Hashtable<String,String> hashMaxHeight = new java.util.Hashtable<String,String>();
        for(int nchart = 0;null != report && null != report.getChartModels() && nchart < report.getChartModels().size();nchart++)
        {
            Chart chart = report.getChartModels().get(nchart);
            int nrowIndex = chart.getRowIndex();
            String strHeight = chart.getHeight();
            strHeight = strHeight == null ? "" : strHeight;
            String strMaxHeight = (String) hashMaxHeight.get("" + nrowIndex);
            if(strMaxHeight == null)
            {
                hashMaxHeight.put("" + nrowIndex,strHeight);
                roweindexArray.add(nrowIndex);
                continue;
            }
            else
            {
                strMaxHeight = strMaxHeight.compareTo(strHeight) < 0 ? strHeight : strMaxHeight;
                hashMaxHeight.put("" + nrowIndex,strMaxHeight);
            }
        }
//        int[] rowindexAry = roweindexArray.toArray();
        java.util.Collections.sort(roweindexArray);
        for(int nmind = 0;nmind < roweindexArray.size();nmind++)
        {
            int nrowindex = roweindexArray.get(nmind);
            String strMaxHeight = (String) hashMaxHeight.get("" + nrowindex);
            writeln("<div style=\"clear:both;border:0;margin:0;padding:0;\">");
            for(int nchart = 0;null != report && null != report.getChartModels() && nchart < report.getChartModels().size();nchart++)
            {
                Chart c = report.getChartModels().get(nchart);
                if(c.getRowIndex() != nrowindex)
                {
                    continue;
                }
                renderChartDiv(log,c,null,strMaxHeight);
            }
            writeln("</div>");
        }
        writeln("<div style=\"clear:both;\"></div>");
        return m_buf.toString();
    }

    private void renderChartDiv(Logger log,Chart chart,String strWidth,String strHeight)
    {
        String newId = "CHART_" + Guid.newGuid();
        String strStyle = "float:left;border:0;padding:0;margin:0;";
        strWidth = strWidth == null ? chart.getWidth() : strWidth;
        strHeight = strHeight == null ? chart.getHeight() : strHeight;
        if(null != strWidth)
        {
            strStyle += "width:" + strWidth + ";";
        }
        else
        {
            strStyle += "width:300px;";
        }
        if(null != strHeight)
        {
            strStyle += "height:" + strHeight + ";";
        }
        else
        {
            strStyle += "height:200px;";
        }
        writeln("<div id=\"" + newId + "\" style=\"" + strStyle + "\">");
        renderChart(log,chart,newId);
        writeln("</div>");

    }

    protected abstract void renderChart(Logger log,Chart model,String divId);

    protected void addJavaScripts(String ...scriptFiles)
    {
        if(null == scriptFiles)
        {
            return;
        }
        for(String str : scriptFiles)
        {
            writeln((new HTMLNode.SCRIPT(str)).toHtml());
        }
    }

    protected void writeln(String str)
    {
        write(str + "\r\n");
    }

    protected void write(String str)
    {
        m_buf.append(str);
    }

}
